<?php
/**
 *    Copyright 2010 Show Framework(www.showframework.cn)
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 * 
 *        http://www.apache.org/licenses/LICENSE-2.0
 * 
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 * 
 * @copyright  Copyright (c) 2009-2010 Show Framework. (http://www.showframework.com)
 * @license    http://www.apache.org/licenses/LICENSE-2.0     Apache License 2.0
 */

/**
 * 缓存类，存储介质为文件
 *
 * @package Show_Cache
 * @name    Show_Cache_File
 * @author  show
 * @since   v0.6
 */
class Show_Cache_File implements Show_Cache_Interface
{
    /**
     * 当前缓存对象的ID
     *
     * @var string
     */
    protected $id;
    
    /**
     * 缓存文件
     *
     * @var string
     */
    protected $path;
    
    /**
     * 有效期
     *
     * @var int
     */
    protected $lifetime;
    
    /**
     * 构造函数
     * 
     * $cache = new Show_Cache_File($id);
     * $cache->save('data', 'id_0001');
     * $cache->save('data', md5($sql));
     *
     * @param string $id
     * @return void
     */
    public function __construct($id)
    {
        $this->id = md5($id);
        $this->path = APP_ROOT . 'data/cache/' . $this->id . '.cache.txt';
        $this->lifetime = false;
    }
    
    /**
     * 设置当前缓存对象的缓存有效期，覆盖之前的设置
     *
     * @param int|false $newLifetimes 新有效期，以秒计算 false为永久有效
     * @return void
     */
    public function setLifetime($newLifetime)
    {
        $this->lifetime = $newLifetime;
    }
    
    /**
     * 用当前缓存对象保存一些数据
     *
     * @param mixed $data 被缓存的数据
     * @param int $lifetime 缓存有效期，不设置为永久有效
     * @return bool 成功返回true，失败返回false
     */
    public function save($data, $lifetime = false)
    {
        $this->lifetime = $lifetime;
        $data = array('create' => time(), 'lifetime' => $this->lifetime, 'data' => $data);
        $string = serialize($data);
        return file_put_contents($this->path, $string, LOCK_EX) === false ? false : true;
    }
    
    /**
     * 加载已缓存的数据
     *
     * @return mixed|null 成功返回缓存的数据，失败返回null
     */
    public function load()
    {
        $data = null;
        if (file_exists($this->path)) {
            $string = file_get_contents($this->path);
            $array = unserialize($string);
            if ($array['lifetime'] === false || ($array['create'] + intval($array['lifetime']) > time())) {
                $data = $array['data'];
            }
        }
        return $data;
    }
    
    /**
     * 移除当前缓存对象
     * 
     * @return void
     */
    public function remove()
    {
        if (file_exists($this->path)) {
            unlink($this->path);
        }
    }
    
    /**
     * 清理缓存
     *
     * @param int $mode
     */
    public static function clean()
    {
        
    }
}
?>